//门店列表
const app = getApp();
var common = require('../../utils/common');
var amapFile = require('../../libs/amap-wx');
var server = require('../../utils/server');
Page({
  data: {
    curCity: "",
    showCity: false,
    animationData: [],
    cityList: [],
    reachableLists: [],
    unreachableLists: [],
    isEmpty: false,
    keyword: "",
    locateCity: "",
    isLocate: true
  },
  onLoad() {
    this.setData({
      locateCity: app.globalData.locateCity
    })
  },
  onShow() {
    this.setData({
      isLocate: app.globalData.isLocate
    })
    server.postJSON(common.storePath + 'store/getcompanytype/', {
      "company_id": app.globalData.companyid
    }, res => {
      if (res.data.errno == "0") {
        this.getAddrList();
      } else {
        common.showErrorToast(res.data.errmsg);
      }
    })
  },
  onUnload: function() {
    wx.removeStorageSync('updateStoreArry');
  },
  getAddrList() {
    common.showLoading()
    var returntype = "";
    if (app.globalData.longitude != null && app.globalData.latitude != null && app.globalData.city != "") {
      this.setData({
        curCity: app.globalData.city
      })
      common.getStoreByLaLon(app.globalData.longitude, app.globalData.latitude, returntype, app.globalData.city, "").then(res => {
        common.hideLoading();
        if (res.errno == '0' && JSON.stringify(res.data) != '{}') {
          var data = res.data,
            reachableLists = [],
            unreachableLists = [];
          for (var i = 0; i < data.length; i++) {
            if (data[i].is_deliverable == 1) {
              reachableLists.push(data[i])
            } else {
              unreachableLists.push(data[i])
            }
          }
          this.setData({
            reachableLists: reachableLists,
            unreachableLists: unreachableLists,
            isEmpty: false
          })

        } else if (JSON.stringify(res.data) == '{}') {
          this.setData({
            isEmpty: true
          })
        } else {
          common.showErrorToast(res.errmsg)
        }
      })
    } else {
      var gdMap = new amapFile.AMapWX({
        key: common.gdKey
      });
      gdMap.getRegeo({
        success: res => {
          app.globalData.location = res[0].regeocodeData.pois[0] == undefined ? res[0].name : res[0].regeocodeData.pois[0].name;
          app.globalData.longitude = res[0].longitude;
          app.globalData.latitude = res[0].latitude;
          app.globalData.city = res[0].regeocodeData.addressComponent.city;
          this.setData({
            curCity: app.globalData.city == "" ? res[0].regeocodeData.addressComponent.city : app.globalData.city
          })
          this.getStoreList(app.globalData.longitude, app.globalData.latitude, returntype, this.data.curCity, app.globalData.storeType, '')


        },
        fail: info => {
          this.setData({
            isLocate: false
          })
          app.globalData.isLocate = false;
          wx.hideLoading();
        }
      })
    }
  },
  getStoreList(longitude, latitude, returntype, city, storetype, keyword) {
    common.getStoreByLaLon(longitude, latitude, returntype, city, storetype, keyword).then(res => {
      var data = res.data,
        reachableLists = [],
        unreachableLists = [];
      if (data.length == 0 || JSON.stringify(res.data) == "{}") {
        this.setData({
          isEmpty: true
        })
      } else {
        this.setData({
          isEmpty: false
        })
      }
      for (var i = 0; i < data.length; i++) {
        if (data[i].is_deliverable == 1) {
          reachableLists.push(data[i])
        } else {
          unreachableLists.push(data[i])
        }
      }
      this.setData({
        reachableLists: reachableLists,
        unreachableLists: unreachableLists
      })
    })
  },
  chooseStore(e) {
    app.globalData.skipType = 2;
    app.globalData.city = this.data.curCity;
    app.globalData.refreshTag = true; //返回是否刷新商品列表页
    wx.setStorage({
      key: "storeId",
      data: e.currentTarget.dataset.storeid
    })
    wx.navigateBack();
  },
  showCityAnimate() {
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: "ease-in",
      delay: 0
    })
    this.animation = animation;
    animation.translateY(700).step();
    this.setData({
      animationData: animation.export(),
      showCity: !this.data.showCity
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  showCityList() {
    common.getCityList().then(res => {
      this.setData({
        cityList: res.data
      })
    })
    this.showCityAnimate();
  },
  hideCityModal() {
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: "ease-in",
      delay: 0
    })
    this.animation = animation;
    animation.translateY(700).step();
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showCity: !this.data.showCity
      })
    }.bind(this), 200)
  },
  selectCity(e) {
    var returntype = "";
    this.setData({
      curCity: e.detail.currentTarget.dataset.name,
      keyword: ''
    });
    this.hideCityModal();
    this.getStoreList(app.globalData.longitude, app.globalData.latitude, returntype, e.detail.currentTarget.dataset.name, "", this.data.keyword)
  },
  getKeyword(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  searchByKeyword() {
    this.getStoreList(app.globalData.longitude, app.globalData.latitude, "", this.data.curCity, "", this.data.keyword)
  },
  openNavigation(e) {
    var la = parseFloat(e.currentTarget.dataset.la),
      lon = parseFloat(e.currentTarget.dataset.lon),
      addr = e.currentTarget.dataset.addr;
    wx.openLocation({
      latitude: la,
      longitude: lon,
      address: addr
    })
  },
  callServer(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel
    })
  },
  // 打开定位
  openLocation(e) {
    if (e.detail.errMsg == "openSetting:ok" && e.detail.authSetting['scope.userLocation']) {
      app.globalData.isLocate = true;
      this.setData({
        isLocate: true
      })
      server.postJSON(common.storePath + 'store/getcompanytype/', {
        "company_id": app.globalData.companyid
      }, res => {
        if (res.data.errno == "0") {
          this.getAddrList();
        } else {
          common.showErrorToast(res.data.errmsg);
        }
      })
    } else {
      common.showErrorToast('需要授权地理位置后使用！');
    }
  },
})